package Solution_4;

import java.util.*;

/**
 * 据用户输入的选择去实现增加、删除、修改、查找以及遍历所有学生信息的功能。
 */
public class StudentSystem {
    private  static String admin = "admin";
    private static String password = "123456";
    private static List<Student> list = new ArrayList<>();
    private static Map<Integer,String> map = new HashMap<>();
    private static Map<Integer,Integer> mlist = new HashMap<>();
   private static Scanner scanner = new Scanner(System.in);
   //登入
    public static void login(){

        //方便演示 就不登入了
       /* System.out.println("这里是学生信息管理系统,请登录...");
        System.out.println("用户名 ：");
        System.out.println(admin = scanner.next());
        System.out.println("密码 ：");
        System.out.println(password = scanner.next());*/
        if(admin.equals("admin") && password.equals("123456")) {
            System.out.println("登入成功！");
            //菜单
            menu();
        }else {
            System.out.println("请重新登入");
            login();
        }

    }
    private static void menu(){
        boolean flag = true;
        while (flag){
            System.out.println("\n这里是学生管理系统\n");
            if(!list.isEmpty()){
                System.out.println("查看学生信息请按1");
                System.out.println("删除学生信息请按2");
                System.out.println("修改学生信息请按3");
            }

            System.out.println("增加学生信息请按4");
            System.out.println("退出系统请按0" );
            int num = 0;
            while ((list.isEmpty() && !String.valueOf(num=scanner.nextInt()).matches("[0|4]")))
                System.out.println("请重新输入");

            while (!list.isEmpty() && !String.valueOf(num=scanner.nextInt()).matches("[0-4]"))
                System.out.println("输入数字无效，请重新输入");
            if (num == 0) {
                System.out.println("拜拜！");
                break;
            }
            switch (num){
                case 0: flag = false;break;
                case 1: show(true);break;
                case 2: delete();break;
                case 3: modify();break;
                case 4: add();break;

            }
        }
    }
    //显示数据
    private static void show(boolean flag) {

        System.out.println("\n\n这里将展示学生信息");
        int i = 0;
        for (Student t:list){
            System.out.println("序号" + i + " | " + t);
            i++;
        }


    }
    //删除
    private static boolean delete() {
        System.out.println("\n\n");
        show(false);
        System.out.println("请输入要删除学生的学号");
        int id = scanner.nextInt();
        while (!map.containsKey(id)){
            System.out.println("该学号不存在。请重新输入！");
            id = scanner.nextInt();
        }
        int idx = mlist.get(id);
        map.remove(id);
        mlist.remove(id);
        list.remove(idx);
        for (int i = idx; i<list.size() ;i++){
            mlist.put(list.get(i).getId(),i);
        }
        System.out.println("删除成功，学生名单：");
        show(false);
       // menu();
        return true;
    }
    //添加
    private static boolean add() {
        //System.out.println("");
        System.out.println("你要添加哪个学生？\n");
        while (true){
            int id,age;
            String name;
            System.out.println("请输入学号 ：");
            id = scanner.nextInt();
            if (map != null && map.containsKey(id)){
                System.out.println("该学号已被占用 请重新输入");
                continue;
            }
            System.out.println("请输入姓名：");
            name = scanner.next();
            System.out.println("请输入年龄：");
            age = scanner.nextInt();
            map.put(id,name);
            mlist.put(id,list.size());
            list.add(new Student(id,name,age));

            break;

        }
     //   menu();

        return true;
    }
    //修改
    private static boolean modify() {
        System.out.println("\n\n");
        show(false);
        System.out.println("\n你要修改哪个学生\n");

        System.out.println("请输入学号 ：");
        int id = scanner.nextInt();
        while (!map.containsKey(id)){
            System.out.println("该学号不存在。请重新输入！");
            id = scanner.nextInt();
        }

        int idx = mlist.get(id);
        System.out.println("请输入姓名：");
        String name = scanner.next();
        System.out.println("请输入年龄：");
        int age = scanner.nextInt();
        list.get(idx).setAge(age);
        list.get(idx).setName(name);
        System.out.println("学号 id 为" + id + "已修改成功！");
       // menu();


        return true;
    }

}
